<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2016-6-21
 * Func   菜单管理
 */
defined('IN_YuLin') || exit('NO PERMIT!');

$table = Table('admin_menu');
if(IS_POST)
{
    //  排序
    if($a == 'sort')
    {
        foreach($_POST['sort'] as $key => $val)
        {
            $db->exec('UPDATE '.$table.' SET sort = '.intval($val).' WHERE id = '.intval($key));
        }

        AjaxReturn(0,'排序成功');
    }    
    
    //  添加 or 修改菜单    
    $id = intval($_REQUEST['id']);
    $data = $id ? $db->getrow('SELECT * FROM '.$table.' WHERE id = '.$id) : [];
    
    //  表单数据
    $post = $_POST['data'];
    
    $ins = [];
    $ins['name']    =   NoBadStr($post['name']);
    $ins['m']       =   NoBadStr($post['m']);
    $ins['c']       =   NoBadStr($post['c']);
    $ins['a']       =   NoBadStr($post['a']);
    $ins['pid']     =   intval($post['pid']);
    $ins['hide']    =   intval($post['hide']);
    $ins['sort']    =   intval($post['sort']);

    if($data['id']) //  修改
    {
        $db->exec('UPDATE '.$table.' SET '.CreateUpdateSql($ins).' WHERE id = '.$data['id']);
        
        //  更改过父级
        if($ins['pid'] != $data['pid'])
        {
            $tmp = $db->getrow('SELECT COUNT(1) AS p FROM '.$table.' WHERE pid = '.$data['pid']);
            if($tmp['p'] < 1)   //  不再有子级
            {
                $db->exec('UPDATE '.$table.' SET son = 0 WHERE id = '.$data['pid']);
            }
        }
    }
    else
    {
        $ins['son'] = 0;
        
        $db->exec('INSERT INTO '.$table.CreateInsertSql($ins));
    }
        
    //  父级分类有孩子了
    $ins['pid'] && $db->exec('UPDATE '.$table.' SET son = 1 WHERE id = '.$ins['pid']);

    AjaxReturn(0,'操作成功');
}

include_once(LIB_PATH.'class/Tree.class.php');

$tree = new Tree();
$result = $db->getall('SELECT * FROM '.$table.' WHERE 1 ORDER BY sort DESC');

if($a == 'add')
{
    $pid = intval($_GET['pid']);    //  添加子分类
    
    $data = [];
    $id = intval($_GET['id']);  //  修改分类
    if($id)
    {
        $data = $db->getrow('SELECT * FROM '.$table.' WHERE id = '.$id);
        $pid = intval($data['pid']);
    }
    
	$array = array();
	foreach($result as $r)
    {
		$r['selected'] = $r['id'] == $pid ? 'selected' : '';
		$array[] = $r;
	}
	$str  = "<option value='\$id' \$selected>\$spacer \$name</option>";
	$tree->init($array);
	$select_categorys = $tree->get_tree(0, $str);
    
    $tpl->display('system/menu_add');
}
elseif($a == 'del')
{
    $id = intval($_GET['id']);
	!$id && AjaxReturn(1,'参数丢失');

    $db->exec('DELETE FROM '.$table.' WHERE id = '.$id);
    AjaxReturn(0,'删除成功');
}
else
{
    $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
    $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
    
    $array = array();
    foreach($result as $r)
    {
    	$r['str_manage'] = '<a href="?m=system&c=menu&a=add&pid='.$r['id'].'">添加子菜单</a> | <a href="?m=system&c=menu&a=add&id='.$r['id'].'">修改</a> | <a data-url="index.php?m=system&c=menu&a=del&id='.$r['id'].'" data-tips="确定删除吗？" href="javascript:void(0);" class="a-del">删除</a> ';
    	$array[] = $r;
    }
    
    $str  = "<tr>
    			<td align='center'><input name='sort[\$id]' type='text' size='3' value='\$sort' class='layui-input'></td>
    			<td align='center'>\$id</td>
    			<td style='text-align:left !important;'>\$spacer\$name</td>
    			<td align='center'>\$str_manage</td>
    		</tr>";
    $tree->init($array);
    $categorys = $tree->get_tree(0, $str);
    
    $tpl->display('system/menu');
}

?>